<!doctype html>
<html>
<head>
<meta charset="utf8">
<link rel="help" href="https://url.spec.whatwg.org/#dom-urlsearchparams-set">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="resources/testharness-extras.js"></script>
<script>
test(function() {
    var params = new URLSearchParams();
    params.append('a', 'b c');
    assert_equals(params + '', 'a=b+c');
    params.delete('a');
    params.append('a b', 'c');
    assert_equals(params + '', 'a+b=c');
}, 'Serialize space');

test(function() {
    var params = new URLSearchParams();
    params.append('a', '');
    assert_equals(params + '', 'a=');
    params.append('a', '');
    assert_equals(params + '', 'a=&a=');
    params.append('', 'b');
    assert_equals(params + '', 'a=&a=&=b');
    params.append('', '');
    assert_equals(params + '', 'a=&a=&=b&=');
    params.append('', '');
    assert_equals(params + '', 'a=&a=&=b&=&=');
}, 'Serialize empty value');

test(function() {
    var params = new URLSearchParams();
    params.append('', 'b');
    assert_equals(params + '', '=b');
    params.append('', 'b');
    assert_equals(params + '', '=b&=b');
}, 'Serialize empty name');

test(function() {
    var params = new URLSearchParams();
    params.append('', '');
    assert_equals(params + '', '=');
    params.append('', '');
    assert_equals(params + '', '=&=');
}, 'Serialize empty name and value');

test(function() {
    var params = new URLSearchParams();
    params.append('a', 'b+c');
    assert_equals(params + '', 'a=b%2Bc');
    params.delete('a');
    params.append('a+b', 'c');
    assert_equals(params + '', 'a%2Bb=c');
}, 'Serialize +');

test(function() {
    var params = new URLSearchParams();
    params.append('=', 'a');
    assert_equals(params + '', '%3D=a');
    params.append('b', '=');
    assert_equals(params + '', '%3D=a&b=%3D');
}, 'Serialize =');

test(function() {
    var params = new URLSearchParams();
    params.append('&', 'a');
    assert_equals(params + '', '%26=a');
    params.append('b', '&');
    assert_equals(params + '', '%26=a&b=%26');
}, 'Serialize &');

function intToHex(i) {
    return (i<0x10 ? '0':'') + i.toString(16).toUpperCase();
}

function urlEncodedSerialize(n) {
  if (n === 0x20) {
    return "\x2B";
  }

  if (n === 0x2A || n === 0x2D || n === 0x2E ||
      (0x30 <= n && n <= 0x39) || (0x41 <= n && n <= 0x5A) ||
      n === 0x5F || (0x61 <= n && n <= 0x7A)) {
    return String.fromCharCode(n);
  }
  var bytes = (new TextEncoder()).encode(String.fromCharCode(n));
  var ret = "";
  for (var i = 0; i < bytes.length; i++) {
      ret += "%" + intToHex(bytes[i]);
  }
  return ret;
}

for (var i = 0x00; i < 0xFF; i++) {
    // Not all bytes can appear in valid UTF-8, so some bytes aren't covered.
    // TODO(mkwst): We fail to properly encode a few bytes: https://crbug.com/557063
    var expected = urlEncodedSerialize(i);
    test(function() {
        var params = new URLSearchParams();
        params.append('' + i, String.fromCodePoint(i));
        assert_equals(params + '', '' + i + '=' + expected);
    }, "Serialize U+00" + intToHex(i) + " -> '" + expected + "'");
}

test(function() {
    var params = new URLSearchParams();
    params.append('a', '\r');
    assert_equals(params + '', 'a=%0D');
}, 'Serialize \\r');

test(function() {
    var params = new URLSearchParams();
    params.append('a', '\n');
    assert_equals(params + '', 'a=%0A');
}, 'Serialize \\n');

test(function() {
    var params = new URLSearchParams();
    params.append('a', '\r\n');
    assert_equals(params + '', 'a=%0D%0A');
}, 'Serialize \\r\\n');

test(function() {
    var params = new URLSearchParams();
    params.append('a', 'b%c');
    assert_equals(params + '', 'a=b%25c');
    params.delete('a');
    params.append('a%b', 'c');
    assert_equals(params + '', 'a%25b=c');
}, 'Serialize %');

test(function() {
    var params = new URLSearchParams();
    params.append('a', 'b\0c');
    assert_equals(params + '', 'a=b%00c');
    params.delete('a');
    params.append('a\0b', 'c');
    assert_equals(params + '', 'a%00b=c');
}, 'Serialize \\0');

test(function() {
    var params = new URLSearchParams();
    params.append('a', 'b\uD83D\uDCA9c');
    assert_equals(params + '', 'a=b%F0%9F%92%A9c');
    params.delete('a');
    params.append('a\uD83D\uDCA9b', 'c');
    assert_equals(params + '', 'a%F0%9F%92%A9b=c');
}, 'Serialize \uD83D\uDCA9');  // Unicode Character 'PILE OF POO' (U+1F4A9)

test(function() {
    var params;
    params = new URLSearchParams('a=b&c=d&&e&&');
    assert_equals(params.toString(), 'a=b&c=d&e=');
    params = new URLSearchParams('a = b &a=b&c=d%20');
    assert_equals(params.toString(), 'a+=+b+&a=b&c=d+');
    // The lone '=' _does_ survive the roundtrip.
    params = new URLSearchParams('a=&a=b');
    assert_equals(params.toString(), 'a=&a=b');
}, 'URLSearchParams.toString');
</script>
</head>
</html>
